<?php

	$TEMP->set_file(array('handle' => DIR_IFRAME_VIEW . 'studentRequestPassword.html'));
	
	$SID = _GET('id');
	
	$getStudentInformation = $STUD->getStudentInformation($SID);
	$name	  	 = $getStudentInformation['last_name'] . ', ' . $getStudentInformation['first_name'] . ' ' . $getStudentInformation['middle_name'];
	$stud_no  	 = $getStudentInformation['stud_no'];
	$studentByhStudNo = $STUD->getStudentByhStudNo($stud_no);
	$email 	  	 = $studentByhStudNo['email'];

	if ( isset($_POST['subButton']) ) {
		
		$if_error		= 0;
		$if_display		= 0;
		$txtusername	= _POST('txtusername');
		$txtnewpass		= _POST('txtnewpass', false); 
		
		if ( empty($txtusername) )	{ $if_error++; $_txtusername 	= REQ_FIELD; }
		if ( empty($txtnewpass) )	{ $if_error++; $_txtnewpass 	= REQ_FIELD; }
		
		if ( $if_error == 0 ) {
			$strArray = array('username' => $txtusername, 'password' => $txtnewpass);
			$STUD->updateStudentRequestPassword($strArray, $SID);
			$STUD->deleteStudentRequestPassword($stud_no);
			
			$httpHost = array('localhost', LOCALHOST);
			
			if(!in_array($_SERVER['HTTP_HOST'], $httpHost) && !empty($email)){
				
				$subject = $name . ' - Reset Password';
				$headers  = 'MIME-Version: 1.0' . "\r\n";
				$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
				$headers .= 'From: do-not-reply <mseuf@yahoo.com>' . "\r\n";
				$message =  '<html><head><title>Reset Password</title></head><body>
							<div style="width:600px">
								<div style="color:#000; font-size:14px; font-family:Verdana, Geneva, sans-serif; font-weight:bold; text-align:left">' . $name . ' - Reset Password</div>
								<div style="font-family:Verdana, Geneva, sans-serif; font-size:12px; text-align:left">
									<span style="font-style:italic">Sent:</span> <u>'.date('l F d, Y').'</u>
								</div>
								<div style="height:2px"></div>
								<div style="font-family:Verdana, Geneva, sans-serif; font-size:12px; padding:5px; display:table; border:1px solid #CCC; width:100%">
									<div style="display:table-row"></div>
										<div style="display:table-cell; width:120px; padding:3px; font-style:italic;">
											Student #: <strong>' . $stud_no . '</strong>
										</div>
										<div style="display:table-row"></div>
										<div style="display:table-cell; padding:3px; font-style:italic">
											Name: <strong>' . $name . '</strong>
										</div>
										<div style="display:table-row"></div>
										<div style="display:table-cell; padding:3px; font-style:italic">
											Email Address: <strong>' . $email . '</strong>
										</div>
										<div style="display:table-row"></div>
										<div style="display:table-cell; padding:3px; font-style:italic">
											Username: <strong>' . $txtusername . '</strong>
										</div>
										<div style="display:table-row"></div>
										<div style="display:table-cell; padding:3px; font-style:italic">
											Your New Password: <strong>' . $txtnewpass . '</strong>
										</div>
								</div>
							</div>
						 </body></html>';
				mail($email, $subject, $message, $headers);
			}
			$saved_record = 1;
		}
	}
	else {
		$txtusername = $getStudentInformation['log_user'];
	}
	
	$TEMP->set_var(array('w_txtusername' 		=> $_txtusername,
						 'w_txtnewpass'			=> $w_txtnewpass,
						 'txtusername'			=> $txtusername,
						 'saved_record'			=> $saved_record,
						 'name'					=> $name,
						 'email'				=> $email,
						 'stud_no'				=> $stud_no,
						 'txtusername'			=> $txtusername
						 ));
?>